package com.mobilesales.android.services;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.reflect.TypeToken;
import com.mobilesales.android.model.exceptions.ServiceUnavailableException;
import com.mobilesales.android.model.tasks.CyclicTask;
import com.mobilesales.android.model.tasks.Schedule;
import com.mobilesales.android.model.tasks.Task;
import com.mobilesales.android.model.tasks.TaskStatus;
import com.mobilesales.android.model.tasks.TaskType;
import com.mobilesales.android.model.wrappedrequests.EndScheduleRequest;
import com.mobilesales.android.model.wrappedrequests.EndTaskRequest;
import com.mobilesales.android.model.wrappedrequests.GetScheduleByDateRequest;
import com.mobilesales.android.model.wrappedrequests.StartScheduleRequest;
import com.mobilesales.android.model.wrappedrequests.StartTaskRequest;
import com.mobilesales.android.model.wrappedrequests.StoreTaskRequest;
import com.mobilesales.android.model.wrappedrequests.UpdateTaskStatusRequest;

public class TaskServiceClient extends ServiceClientBase {
	public TaskServiceClient(Configuration configuration) {
		super(configuration);
	}

	public Task getTask(int taskId) throws ServiceUnavailableException {
		Task task = null;
		String url = getMethodURL() + "/" + String.valueOf(taskId);
		task = (Task) get(url, Task.class);
		return task;
	}

	public List<Task> getTaskList(int scheduleId)
			throws ServiceUnavailableException {
		List<Task> taskList = new ArrayList<Task>();
		List<Object> objectList = null;
		Type listType = new TypeToken<ArrayList<Task>>() {
		}.getType();
		String url = getMethodURL() + "/" + String.valueOf(scheduleId);
		objectList = getList(url, listType);
		if (objectList != null) {
			for (Object obj : objectList) {
				taskList.add((Task) obj);
			}
		} else
			return null;
		return taskList;
	}

	public CyclicTask getCyclicTask(int cyclicTaskId)
			throws ServiceUnavailableException {
		CyclicTask cyclicTask = null;
		String url = getMethodURL() + "/" + String.valueOf(cyclicTaskId);
		cyclicTask = (CyclicTask) get(url, CyclicTask.class);
		return cyclicTask;
	}

	public List<CyclicTask> getCyclicTaskList(int employeeId)
			throws ServiceUnavailableException {
		List<CyclicTask> cyclicTaskList = new ArrayList<CyclicTask>();
		List<Object> objectList = null;
		Type listType = new TypeToken<ArrayList<CyclicTask>>() {
		}.getType();
		String url = getMethodURL() + "/" + String.valueOf(employeeId);
		objectList = getList(url, listType);
		if (objectList != null) {
			for (Object obj : objectList) {
				cyclicTaskList.add((CyclicTask) obj);
			}
		} else
			return null;
		return cyclicTaskList;
	}

	public void storeTask(Task task, int employeeId)
			throws ServiceUnavailableException {
		StoreTaskRequest req = new StoreTaskRequest(task, employeeId);
		String url = getMethodURL();
		post(url, req);
	}

	public void storeCyclicTask(CyclicTask task)
			throws ServiceUnavailableException {
		String url = getMethodURL();
		post(url, task);
	}

	public void updateTaskStatus(int taskId, int taskStatus)
			throws ServiceUnavailableException {
		UpdateTaskStatusRequest req = new UpdateTaskStatusRequest(taskId,
				taskStatus);
		String url = getMethodURL();
		post(url, req);
	}

	public void startTask(int taskId, int statusId, double latitude,
			double longitude) throws ServiceUnavailableException {
		StartTaskRequest req = new StartTaskRequest(taskId, statusId, latitude,
				longitude);
		String url = getMethodURL();
		post(url, req);
	}

	public void endTask(int taskId, int statusId, double latitude,
			double longitude) throws ServiceUnavailableException {
		EndTaskRequest req = new EndTaskRequest(taskId, statusId, latitude,
				longitude);
		String url = getMethodURL();
		post(url, req);
	}

	public List<TaskStatus> getTaskStatusList()
			throws ServiceUnavailableException {
		List<TaskStatus> taskStatusList = new ArrayList<TaskStatus>();
		List<Object> objectList = null;
		Type listType = new TypeToken<ArrayList<TaskStatus>>() {
		}.getType();
		String url = getMethodURL();
		objectList = getList(url, listType);
		if (objectList != null) {
			for (Object obj : objectList) {
				taskStatusList.add((TaskStatus) obj);
			}
		} else
			return null;
		return taskStatusList;
	}

	public TaskStatus getTaskStatus(int taskStatusId)
			throws ServiceUnavailableException {
		TaskStatus taskStatus = null;
		String url = getMethodURL() + "/" + String.valueOf(taskStatusId);
		taskStatus = (TaskStatus) get(url, TaskStatus.class);
		return taskStatus;
	}

	public List<TaskType> getTaskTypeList() throws ServiceUnavailableException {
		List<TaskType> taskTypeList = new ArrayList<TaskType>();
		List<Object> objectList = null;
		Type listType = new TypeToken<ArrayList<TaskType>>() {
		}.getType();
		String url = getMethodURL();
		objectList = getList(url, listType);
		if (objectList != null) {
			for (Object obj : objectList) {
				taskTypeList.add((TaskType) obj);
			}
		} else
			return null;
		return taskTypeList;
	}

	public TaskType getTaskType(int taskTypeId)
			throws ServiceUnavailableException {
		TaskType taskType = null;
		String url = getMethodURL() + "/" + String.valueOf(taskTypeId);
		taskType = (TaskType) get(url, TaskType.class);
		return taskType;
	}

	public Schedule getSchedule(int scheduleId)
			throws ServiceUnavailableException {
		Schedule schedule = null;
		String url = getMethodURL() + "/" + String.valueOf(scheduleId);
		schedule = (Schedule) get(url, Schedule.class);
		return schedule;
	}

	public Schedule getScheduleByDate(int employeeId, String date)
			throws ServiceUnavailableException {
		Schedule schedule = null;
		GetScheduleByDateRequest req = new GetScheduleByDateRequest(employeeId,
				date);
		String url = getMethodURL();
		schedule = (Schedule) postReturn(url, req, Schedule.class);
		return schedule;
	}

	public List<Schedule> getScheduleList(int employeeId)
			throws ServiceUnavailableException {
		List<Schedule> scheduleList = new ArrayList<Schedule>();
		List<Object> objectList = null;
		Type listType = new TypeToken<ArrayList<Schedule>>() {
		}.getType();
		String url = getMethodURL() + "/" + String.valueOf(employeeId);
		objectList = getList(url, listType);
		if (objectList != null) {
			for (Object obj : objectList) {
				scheduleList.add((Schedule) obj);
			}
		} else
			return null;
		return scheduleList;
	}

	public void startSchedule(int scheduleId, double latitude, double longitude)
			throws ServiceUnavailableException {
		StartScheduleRequest req = new StartScheduleRequest(scheduleId,
				latitude, longitude);
		String url = getMethodURL();
		post(url, req);
	}

	public void endSchedule(int scheduleId, double latitude, double longitude)
			throws ServiceUnavailableException {
		EndScheduleRequest req = new EndScheduleRequest(scheduleId, latitude,
				longitude);
		String url = getMethodURL();
		post(url, req);
	}

}
